package com.kitty.game.xmd;


import com.kitty.common.utils.SpringUtils;
import com.kitty.game.child.model.ChildInfo;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.enter.RespGeneralNotify;
import com.kitty.game.enter.RespUpdate;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.model.Role;
import com.kitty.game.utils.NotifyModules;
import com.kitty.common.model.Reason;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class AssingXmdController{
    Logger logger = LoggerFactory.getLogger(AssingXmdController.class);

    @RequestMapping
    public void xianmo_xmd(IoSession session, AssingXmdInfo assingXmdInfo) {
        Role role = SessionUtils.getRoleBySession(session);
        int xian = assingXmdInfo.getXian();
        int mo = assingXmdInfo.getMo();

        int mountXian = 0;
        int mountMo = 0;

        /**坐骑增益*/
        if (role.getMount() >= 1) {
            Pet mount = SpringUtils.getPetService().getPetById(role.getMount(), role);
            if (mount != null) {
                int currTime = new Long(new Date().getTime() / 1000).intValue();
                if (mount.getMountTime() > currTime) {//风灵丸时间大于当前时间才给计算增益属性
                    List<FiedValue> fieldList = SpringUtils.getBean(PetService.class).getMountAttrib(mount).getFieldValues();
                    for (FiedValue fiedValue : fieldList) {
                        if(fiedValue.getType() == 925){
                            mountXian = Integer.parseInt(fiedValue.getValue().toString());
                        }
                        if(fiedValue.getType() == 926){
                            mountMo = Integer.parseInt(fiedValue.getValue().toString());
                        }

                    }
                }
            }
        }

        RespGeneralNotify respGeneralNotify = new RespGeneralNotify();
        respGeneralNotify.setNotify((short) NotifyModules.NOTIFY_ASSIGN_XMD);
        respGeneralNotify.setValue("1");
        MessagePusher.pushMessage(session, respGeneralNotify);

        ChildInfo info = role.getChildInfo();
        int goldCoin = 0;
        if (xian > 3000) {
            xian -= 65536;
            goldCoin += Math.abs(xian) * 328;
        }
        if (mo > 3000) {
            mo -= 65536;
            int minPoint = Math.max(role.getLevel() - 119, 0);
            goldCoin += Math.abs(mo) * 328;
        }
        int infoXian = info.getXian();
        infoXian+=xian;
        info.setXian(infoXian);

        int infoMo = info.getMo();
        infoMo+=mo;
        info.setMo(infoMo);

        int infoTotal  = info.getUpgradeTotal();
        infoTotal -=xian+mo;
        info.setUpgradeTotal(infoTotal);

        SpringUtils.getRoleService().subtractGold(role, goldCoin, Reason.XIAN_MO);
        List<FiedValue> list = new ArrayList<>();
        list.add(new FiedValue(925, info.getXian()+mountXian));
        list.add(new FiedValue(926, info.getMo()+mountMo));
        list.add(new FiedValue(927, info.getUpgradeTotal()));
        RespUpdate respUpdate = new RespUpdate();
        respUpdate.setRoleId(role.getRoleId());
        respUpdate.setList(list);
        MessagePusher.pushMessage(role, respUpdate);
        role.save();
    }
}
